<?php
use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $content string 字符串 */


$web = Url::to("@web");

?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <title>购物车</title>
    <link type="text/css" href="<?= $web ?>/css/wx-good/global.css" rel="stylesheet"/>
    <link type="text/css" href="<?= $web ?>/css/wx-good/shopping-cart.css" rel="stylesheet"/>
    <script type="text/javascript" src="<?= $web ?>/js/jquery.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/tools.js"></script>
    <?php require_once Yii::$app->viewPath . '/wx-no-share.php' ?>
    <script type="text/javascript">
        $(document).ready(function () {
            var w = $(document).width();
            if (w > 640) {
                w = 640;
            }
            $("html").css("font-size", w * 20 / 320);
            $(".text_box").each(function () {
                if ($(this).val() <= 1) {
                    $(".reduce").css("display", "block");
                    $(".reduce-2").css("display", "none")
                } else {
                    $(".reduce").css("display", "none");
                    $(".reduce-2").css("display", "blcok")
                }
            })
        });

        var totalnum = 0;
        var good_tpl = "";
        $(function ($) {
            good_tpl = returnstring("<?=$web?>/js/wx-good/cart-goods.tpl");
            $(".text_box").each(function () {
                if ($(this).val() <= 1) {
                    $(".reduce").css("display", "block");
                    $(".reduce-2").css("display", "none")
                } else {
                    $(".reduce").css("display", "none");
                    $(".reduce-2").css("display", "blcok")
                }
            });
            loadCartList();
        });
        function loadCartList() {
            var data = {};
            requestJson("<?=Url::to(['wx-good/get-goods-cart-handle'])?>", data, true, function (response) {
                if (response.status == 0) {
                    var target = $("#v_cart");
                    target.empty();
                    totalnum = response.data.TOTALNUM;
                    $.each(response.data.cartList, function (i, item) {
                        var dom = document.createElement("DIV");
                        dom.innerHTML = good_tpl._format({
                            pic: item.IMAGE_URL,
                            code: item.GOODS_CODE,
                            name: item.GOODS_NAME,
                            num: item.COUNT,
                            stock: item.STOCK,
                            price: parseFloat(item.PRICE),
                            web: '<?=$web?>'
                        });
                        target.get(0).appendChild(dom);
                        checked_length = $(".commodity-foot-bottom>.choice-checkbox").length;
                        checked_click();
                    });

                    loadPayData();
                } else {
                    alert(response.message);
                }
            });
        }

        var checked_number = 0;
        var checked_length = 0;
        function checked_click() {
            var input_checked = $(".commodity-foot-bottom>.choice-checkbox");
            input_checked.each(function (index) {
                $(this).click(function () {
                    if ($(this).is(':checked') == false) {
                        $("#all_checkbox").prop('checked', false);
                    }
                    checked_number = 0;
                    input_checked.each(function (index) {
                        if ($(this).is(':checked') == true) {
                            checked_number += 1;
                        }
                        if (checked_number == checked_length) {
                            $("#all_checkbox").prop('checked', true);
                        }
                    });
                    loadPayData();
                });
            })
        }

        var goods_code_list = [];
        function loadPayData() {
            var total_num = 0;
            var totalprice = 0;
            goods_code_list = [];
            var input_checked = $(".commodity-foot-bottom>.choice-checkbox");
            input_checked.each(function () {
                if ($(this).is(':checked') == true) {
                    var goods_count = $(this).parent().parent().parent().find('input[class*=text_box]');
                    var goods_price = $(this).parent().parent().parent().find('input[class*=hidden_GOODS_PRICE]');
                    var goods_code = $(this).parent().parent().parent().find('input[class*=hidden_GOODS_CODE]');
                    total_num += parseInt(goods_count.val());
                    totalprice += parseFloat(goods_count.val()) * parseFloat(goods_price.val());
                    goods_code_list.push(goods_code.val());
                }
            });
            $('#v_count').html("(" + total_num + ")");
            $('#v_totalprice').html(totalprice);
        }

        function addnum(me, stock) {
            var t = $(me).parent().parent().find('input[class*=text_box]');
            var v = parseInt(t.val());
            if (v >= stock) {
                alert("库存数量不足！");
                return;
            }

            if (v > 99) {
                alert("买的太多了！");
                return;
            }
            var code = $(me).parent().parent().find('.hidden_GOODS_CODE').val();
            var rtstatus = incNum(code, 1);
            if (rtstatus == true) {
                var afternum = v + 1;
                t.val(afternum);
                $(me).parent().parent().find('.hidden_GOODS_NUM').val(afternum);
                if (afternum == 1) {
                    $(me).parent().parent().find('.top-choice-left').addClass('top-choice-button');
                } else {
                    $(me).parent().parent().find('.top-choice-left').removeClass('top-choice-button');
                }
            }
            //加载支付数据
            loadPayData();
        }

        function decnum(me) {
            var t = $(me).parent().parent().find('input[class*=text_box]');
            var v = parseInt(t.val());
            if (v <= 1) {
                alert("数量不够减了");
                return;
            }
            var code = $(me).parent().parent().find('.hidden_GOODS_CODE').val();
            var rtstatus = incNum(code, -1);
            if (rtstatus == true) {
                var afternum = v - 1;
                t.val(afternum);
                if (afternum == 1) {
                    $(me).addClass('top-choice-button');
                } else {
                    $(me).removeClass('top-choice-button');
                }
            }
            //加载支付数据
            loadPayData();
        }

        function incNum(goods_code, addnum) {
            var data = {};
            data.goods_code = goods_code;
            data.addnum = addnum;
            var response = requestJson("<?=Url::to(['wx-good/inc-goods-to-cart-handle'])?>", data);
            if (response.status == 0) {
                return true;
            } else {
                alert(response.message);
                return false;
            }
        }

        function update_goods_num(me, stock) {
            var code = $(me).parent().find('.hidden_GOODS_CODE').val();
            var old_num = $(me).parent().find('.hidden_GOODS_NUM').val();
            var new_num = $(me).parent().find('.text_box').val();

            var hidnum = $(me).parent().find('.hidden_GOODS_NUM');
            var inputnum = $(me).parent().find('input[class*=text_box]');

            if (old_num == new_num) {
                return;
            } else if (new_num <= 0) {
                alert('数量不能小于等于0！');
                inputnum.val(old_num);
                return;
            } else if (new_num > stock) {
                alert('库存数量不足！');
                inputnum.val(old_num);
                return;
            } else if (new_num > 99) {
                alert('买的太多了！');
                inputnum.val(old_num);
                return;
            }

            //计算出需要加或者减的数量
            var upnum = new_num - old_num;
            var rtstatus = incNum(code, upnum);
            if (rtstatus == true) {
                hidnum.val(new_num);
                if (new_num == 1) {
                    $(me).parent().parent().find('.top-choice-left').addClass('top-choice-button');
                } else {
                    $(me).parent().parent().find('.top-choice-left').removeClass('top-choice-button');
                }
            }
            //加载支付数据
            loadPayData();
        }

        var currentItem = null;
        function del_goods(me) {
            currentItem = me;

            $("#confirm_dialog").show();
        }

        function doDelGoods() {
            $("#confirm_dialog").hide();

            me = currentItem;
            var code = $(me).parent().parent().parent().find('.hidden_GOODS_CODE').val();
            var data = {};
            data.goods_code = code;
            requestJson("<?=Url::to(['wx-good/del-goods-to-cart-handle'])?>", data, true, function (response) {
                if (response.status == 0) {
                    loadCartList();

                } else {
                    alert(response.message);
                }
            });
        }

        function create_order() {
            if (goods_code_list == null || goods_code_list.length <= 0) {
                alert("请选择需要结算的产品");
                return;
            }
            var data = {};
            data.goodslist = goods_code_list;
            var re = requestJson("<?=Url::to(['wx-good/update-cart-goods-selected'])?>", data);
            if (re.status == 0) {
                //购物车产品状态修改成功 跳转至订单确认页面
                window.location.href = "<?=Url::to(["wx-good/create-cart-to-order-handle"]);?>";
            } else {
                alert(re.message);
            }
        }

        function href_home() {
            window.location.href = "<?=Url::to(["wx-good/goods-list"]);?>";
        }

        function clear_cart() {
            if (totalnum <= 0) return;
            var cf = confirm('确定要清空吗?');
            if (cf == true) {
                var data = {};
                var re = requestJson("<?=Url::to(['wx-good/clear-cart-handle'])?>", data);
                if (re.status == 0) {
                    loadCartList();
                } else {
                    alert(re.message);
                }
            }
        }

    </script>

</head>
<body>
<div class="main" style="width: 16rem; margin: 0 auto;">
    <div class="head">
        <div class="head-div">
            <div class="head-radius"><input id="all_checkbox" class="choice-checkbox" type="checkbox"/></div>
            <div class="head-words"><span>全选</span></div>
        </div>
        <div class="head-div head-div-clear" onclick="clear_cart();">
            <div class="head-radius-2">
<!--                <img src="--><?//= $web ?><!--/img/wx-good/shopping-cart-1.png"-->
<!--                     style="width: 100%;height:100%;">-->
            </div>
            <div class="head-words"><span>清空购物车</span></div>
        </div>
    </div>
    <div class="hr"></div>


    <div id="v_cart">

    </div>

    <script>
        $("#all_checkbox").click(function () {
            if ($("#all_checkbox").is(':checked') == true) {
                $(".commodity-foot-bottom>.choice-checkbox").prop('checked', true);
                loadPayData();
            } else {
                $(".commodity-foot-bottom>.choice-checkbox").prop('checked', false);
                loadPayData();
            }
        })
    </script>

    <div class="foot-margin"></div>
    <div class="foot">
        <div class="foot-left">
            需支付：￥<span id="v_totalprice">0</span>
        </div>
        <div class="foot-middle">
            <div class="foot-middle-big" onclick="href_home();">
                <img src="<?= $web ?>/img/wx-good/shopping-cart-6.png">
            </div>
        </div>
        <div class="foot-right" onclick="create_order();">
            结算支付&nbsp;<span id="v_count">(0)</span>
        </div>
    </div>
</div>

<!--BEGIN dialog confirm-->
<div id="confirm_dialog" class="weui_dialog_confirm" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd">
            <div class="weui_dialog_title">是否移除此商品?</div>
        </div>
        <div class="weui_dialog_bd">
        </div>
        <div class="weui_dialog_ft">
            <a href="javascript:;" class="weui_btn_dialog default"
               onclick="javascript:$('#confirm_dialog').hide();">取消</a>
            <a href="javascript:;" class="weui_btn_dialog primary" onclick="doDelGoods();">确定</a>
        </div>
    </div>
</div>
<!--END dialog confirm-->
</body>
</html>